package com.l.web.action.system;

import com.l.web.master.system.webnode.WebNode;
import com.l.web.master.system.webnode.WebNodeDAO;
import com.l.web.master.webcontent.WebContent;
import com.l.web.master.webcontent.WebContentDAO;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.l.qiao.Cluster;
import org.l.qiao.Page;
import org.l.qiao.WebNodeOption;
import org.l.qiao.check.WebNodeOptionCheck;
import org.l.qiao.dao.WebNodeOptionDAOAbstract;
import org.l.qiao.search.Query;
import org.l.qiao.search.Sort;
import org.l.qiao.web.ResponseCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class WebNodeContentService {

    private HttpServletResponse res;
    private HttpServletRequest req;
    private static final Logger LOGGER = Logger.getLogger(WebNodeContentService.class);
    private WebNodeOptionCheck check = new WebNodeOptionCheck();
    private WebNodeDAO channeldao;
    private WebNodeOptionDAOAbstract channeloptiondao;
    private WebContentDAO channelcontentdao;
    private List<Cluster> channeloptions = null;

    private int pagenumber = 1;
    private int pagecount = 20;
    private Cluster rslist = null;
    private WebNode webNode;
    private WebContent channelcontent = null;

    private String id = "";
    private String channelid = "";

    public void setWebNodedao(WebNodeDAO channeldao) {
        this.channeldao = channeldao;
    }

    public void setWebNodeoptiondao(WebNodeOptionDAOAbstract channeloptiondao) {
        this.channeloptiondao = channeloptiondao;
    }

    public void setWebNodecontentdao(WebContentDAO channelcontentdao) {
        this.channelcontentdao = channelcontentdao;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setWebNodeid(String channelid) {
        this.channelid = channelid;
    }

    public void setPagenumber(int pagenumber) {
        this.pagenumber = pagenumber;
    }

    public void setPagecount(int pagecount) {
        this.pagecount = pagecount;
    }

    public String execute() throws IOException {
        if (check.checkWebNodeid(channelid)) {
            WebContent cc = new WebContent();
            Query query = new Query();
            webNode = new WebNode(channeldao.findById(channelid));
            switch (webNode.getString(webNode.TYPE, "")) {
                case "article": // 内容显示
                    query.eq(cc.WEB_NODE_ID, channelid);
                    channelcontent = new WebContent(channelcontentdao.findOne(query));
                    return edit();
                case "list":
                    query.eq(cc.WEB_NODE_ID, channelid);
                    WebContent field = new WebContent();
                    field.put("title", 1);
                    field.put(field.CREATED, 1);
                    Page page = new Page();
                    page.setSize(pagecount);
                    page.setIndex(pagenumber);
                    rslist = channelcontentdao.findPage(query, field, null, page);
                    return "list";

				/*
                 * mcs.channelidEQ(channelid); WebNodeOptionSearch mos = new
				 * WebNodeOptionSearch(); mos.listEQ("1"); WebNodeOption mofield
				 * = new WebNodeOption(); mofield.put(mofield.Name, 1);
				 * List<Map<LString, Object>> listoption =
				 * channeloptiondao.find(mos, mofield); WebNodeContent
				 * channelfield = new WebNodeContent(); for (Map<LString, Object>
				 * map : listoption) { try {
				 * channelfield.put(map.get(mofield.Name).toString(), 1); }
				 * catch (Exception e) { LOGGER.error("错误选项"); } }
				 * channelfield.put(channelfield.Created, 1); WebNodeContent
				 * channelsort = new WebNodeContent();
				 * channelsort.put(channelsort.Created, 1); rslist =
				 * channelcontentdao.finds(mcs, channelfield, channelsort,
				 * pagenumber, pagecount, "channelcontent"); return "list";
				 */

                case "url":
                    return "url";
                default:
                    return "url";
            }
        } else {
            return "NONE";
        }
    }

    public String edit() throws IOException {
        if (check.checkWebNodeid(channelid)) {
            WebNodeOption co = new WebNodeOption();
            Query query = new Query();
            query.eq(co.WebNodeId, channelid);
            Sort sort = new Sort();
            sort.desc(co.Index);
            channeloptions = channeloptiondao.find(query, null, sort);
            if (check.checkId(id)) {
                channelcontent = new WebContent(channelcontentdao.findById(id));
            } else if (channelcontent == null) {
                channelcontent = new WebContent();
            }
            return "edit";
        } else {
            res.sendError(404);
            LOGGER.error("非法编辑模块内容：channelid=" + channelid);
            return "NONE";
        }
    }

    public void save() throws IOException {
        channelcontent = new WebContent();
        boolean chrs = check.checkWebNodeid(channelid);
        if (chrs) {
            WebNodeOption co = new WebNodeOption();
            Query query = new Query();
            query.eq(co.WebNodeId, channelid);
            Sort sort = new Sort();
            sort.desc(co.Name);
            List<Cluster> keys = channeloptiondao.find(query, null, sort);
            for (Map<String, Object> map : keys) {
                String key = map.get(co.Name).toString();
                if (!channelcontent.checkput(key, req.getParameter(key))) {
                    chrs = false;
                    break;
                }
            }
        }
        if (chrs) {
            if (check.checkId(id)) {
                Query query = new Query();
                query.idEQ(id);
                channelcontentdao.updateOne(query, channelcontent);
            } else if (id.equals("")) {
                id = channelcontent.setId();
                channelcontent.put(channelcontent.WEB_NODE_ID, channelid);
                channelcontent.put(channelcontent.CREATED, new Date());
                channelcontentdao.insert(channelcontent);
            } else {
                res.sendError(404);
                LOGGER.info("非法添加模块信息:channelid=" + channelid + ",id=" + id);
            }

            JSONObject json = new JSONObject();
            json.put("code", ResponseCode.FINISH);
            json.put("id", id);
            res.getWriter().write(json.toString());
        } else {
            res.sendError(404);
            LOGGER.info("非法添加模块信息:channelid=" + channelid);
        }
    }

    public void delete() throws IOException {
        if (check.checkId(id)) {
            int code = ResponseCode.FINISH;
            channelcontentdao.removeById(id);
            JSONObject json = new JSONObject();
            json.put("code", code);
            res.getWriter().write(json.toString());
        } else {
            LOGGER.error("非法删除模块内容：id=" + id);
            res.sendError(404);
        }
    }

    public List<Cluster> getWebNodeoptions() {
        return channeloptions;
    }

    public Map<String, Object> getWebNodecontent() {
        return channelcontent;
    }

    public Map<String, Object> getRslist() {
        return rslist;
    }

    public String getWebNodeid() {
        return channelid;
    }

}
